<template>
  <div>
    <el-card class="box-card">
      <h2 class="titleHeader">
        {{ route.query.id ? '修改说说' : '发表说说' }}
      </h2>
      <div style="width:95%;margin:auto">
        <el-input v-model="talkText" :rows="6" type="textarea" placeholder="请输入内容" />
      </div>
      <div class="flexReight">

        <V3Emoji @click-emoji="appendText" />

        <el-select v-model="selectVal" class="m-2">
          <el-option label="公开" value="1" />
          <el-option label="私密" value="0" />
        </el-select>

        <button @click="pushTalkHandle">
          <p>send</p>
          <svg stroke-width="4" stroke="currentColor" viewBox="0 0 24 24" fill="none" class="h-6 w-6"
            xmlns="http://www.w3.org/2000/svg">
            <path d="M14 5l7 7m0 0l-7 7m7-7H3" stroke-linejoin="round" stroke-linecap="round"></path>
          </svg>
        </button>

      </div>
    </el-card>
  </div>
</template>

<script  setup lang="ts">
import V3Emoji from 'vue3-emoji'
import 'vue3-emoji/dist/style.css'
import { ElMessage } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
import { ref } from 'vue'

import { pushTalk, getTalkSingle, updateTalk } from '@/utils/talkHttp'
const selectVal = ref('1')
const talkText = ref('')

const route = useRoute()
const router = useRouter()
// 获取单个数据修改数据
function getTalkSingleData(): void {
  getTalkSingle(route.query.id as string).then(res => {
    if (!res.data.ok) return;
    selectVal.value = res.data.data[0].type
    talkText.value = res.data.data[0].talk


  }).catch(err => ElMessage.error('服务器错误没找到'))
}
route.query.id && getTalkSingleData()

const appendText = (val: string) => {
  talkText.value += val;
};
const pushTalkHandle = () => {

  if (!talkText.value.trim()) return ElMessage.error('动态不能为空')
  if (route.query.id) {
    updateTalk(route.query.id as string, talkText.value, selectVal.value).then(res => {
      if (res.data.ok) {
        router.back()
        ElMessage({
          message: res.data.info,
          type: 'success',
        })
      } else {
        ElMessage.error(res.data.info)
      }
    }).catch(err => ElMessage.error('服务器错误 请检查token'))
  } else {
    pushTalk(talkText.value, selectVal.value).then(res => {
      talkText.value = ''
      selectVal.value = '1'
      if (res.data.ok) {
        ElMessage({
          message: res.data.info,
          type: 'success',
        })
      } else {
        ElMessage.error(res.data.info)
      }
    }).catch(err => ElMessage.error('服务器错误 请检查token'))
  }
}
</script>

<style lang="scss" scoped>
.titleHeader {
  margin-bottom: 20px;
}

.m-2 {
  border: 0;
  width: 100px;
}

.flexReight {
  width: 95%;
  display: flex;
  justify-content: space-between;
  margin: 20px auto;
  padding: 5px;

  button {
    padding: 0;
    margin-left: 20px;
    border: none;
    background: none;
  }

  button {
    --primary-color: #111;
    --hovered-color: #c84747;
    position: relative;
    display: flex;
    font-weight: 600;
    font-size: 20px;
    gap: 0.5rem;
    align-items: center;
    width: 80px;
  }

  button p {
    margin: 0;
    position: relative;
    font-size: 20px;
    color: var(--primary-color)
  }

  button::after {
    position: absolute;
    content: "";
    width: 0;
    left: 0;
    bottom: -7px;
    background: var(--hovered-color);
    height: 2px;
    transition: 0.3s ease-out;
  }

  button p::before {
    position: absolute;
    /*   box-sizing: border-box; */
    content: "send";
    width: 0%;
    inset: 0;
    color: var(--hovered-color);
    overflow: hidden;
    transition: 0.3s ease-out;
  }

  button:hover::after {
    width: 100%;
  }

  button:hover p::before {
    width: 100%;
  }

  button:hover svg {
    transform: translateX(4px);
    color: var(--hovered-color)
  }

  button svg {
    color: var(--primary-color);
    transition: 0.2s;
    position: relative;
    width: 15px;
    transition-delay: 0.2s;
  }
}

.box-card {
  overflow: visible;
}
</style>